/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
define("pages/bgcl/bgcl_jbxx_form", [
        "../../plugins/easyui/jquery-easyui-1.5.1/jquery.easyui.min",
        "../../plugins/easyui/jquery-easyui-1.5.1/easyloader",
        '../../pages/jls_jbxx/personInfoForm.html.js',
        '../../pages/bgcl/bgcl_form.html.js',
        "../../common/componts",
        "../../common/tools"
    ],
    function(require){
        var layerIndex;
        var _callback;

        var personInfoFormTemplate = require("../../pages/jls_jbxx/personInfoForm.html");
        var formTemplate = require("./bgcl_form.html");
        formTemplate = formTemplate.replace('<div class="jbxx-placeholder"></div>',personInfoFormTemplate);

        /**
         * 获取表单内容
         * @param initData
         * @returns {*}
         */
        var getFormHtml = function(initData){
            var htmlCompile = template.compile(formTemplate);
            var username = CURRENT_USER.username;//获取当前办理人
            var html = htmlCompile({
                tbr:username,
                tbrq:currentDate(),
                data:initData
            });
            return html;
        };
        /**
         * 动态渲染
         */
        var parseEasyUIElement = function(){
            $.parser.parse($(".awd-form-panel"));
        };

        var _show = function(initData,callback){
            _callback = callback;
            var componts = require("../../common/componts");

            layerIndex = layer.open({
                type: 1,
                title: "数据录入",
                skin: 'layui-layer-lan', //加上边框
                closeBtn: 1, //不显示关闭按钮
                shadeClose: true, //开启遮罩关闭
                content: getFormHtml(initData),
                area:['100%','100%'],
                success: function(layero, index){
                    console.log(layero, index);
                }
            });
            layer.full(layerIndex);
            setTimeout(function(){
                //初始化下拉框
                componts.initSelectPage(undefined);
                componts.initSelect(undefined,undefined,undefined);
                parseEasyUIElement(); //动态渲染
            },100);
        };

        var save = function(){
            var url = "/bgcl/save";
            var $form = $(".awd-form");
            if (!$form.form('validate')) {
                $.messager.alert("提示", '必填项不能为空！');
                return;
            }
            var data = $form.serialize();
            console.log('form data -> ',data);
            var loadingIndex = layer.load();
            $.ajax({
                type : "POST",  //提交方式
                url : url,
                data : data,//数据，这里使用的是Json格式进行传输
                success : function(result) {
                    layer.closeAll();
                    if (result.success) {
                        _callback && _callback();
                        $.messager.alert("确定","保存成功","info");
                    }else{
                        $.messager.alert("确定","保存失败","warn");
                    }
                },
                complete:function(){
                    layer.close(loadingIndex);
                }
            });
        };

        var bindAction = function(){
            //保存按钮事件
            $(document).on('click','.save-button',function () {
                save();
            });
            //点击关闭按钮
            $(document).on('click','.close-button',function(){
                layer.close(layerIndex);
                layerIndex = null;
            });
            //修正layer 最大化，解决办法..
            $(window).resize(function(){
                layerIndex && layer.full(layerIndex);
                parseEasyUIElement();//TODO::后续优化
            })
        };

        (function(){
            var componts = require("../../common/componts");
            componts.initSelectPage(undefined);
            componts.initSelect(undefined,undefined,undefined);
            bindAction();
        })()

        return{
            show:_show
        };
    });